<?php
            
use app\model\Theme; 



class Config_Page extends PluginWebPage {
    function main(){
         global $_G,$_P,$_S,$_C;
         $system = $_S;
         $config = $_C;
         $method = 'gift_config';
         $themeid = intval($_P['themeid']);
         if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
         }
         $themelist = Theme::getAllTheme();
         $theme = Theme::getThemeById($themeid);
         $gift_config = db_get(t_intive_gift_config, array('software'=>MODULE_NAME_TABEL,'themeid'=>$themeid));
         $is_new_wxapp_version = $system['is_new_wxapp'];
         if($_G['ispost']){
             $tuan_gift_lastid = $gift_config['tuan_gift_lastid']?$gift_config['tuan_gift_lastid']:0;
             $invite_gift_lastid = $gift_config['invite_gift_lastid']?$gift_config['invite_gift_lastid']:0;
             $help_gift_lastid =  $gift_config['help_gift_lastid']?$gift_config['help_gift_lastid']:0;
             $msg_array = [];
             //成团礼
             $identity = GiftModel::identity();

             $open_tuan_gift = 1;
             $data['open_tuan_gift'] = $open_tuan_gift;
             $tuan_taskid = [];
             if(isset($_P['tuan_taskid'])){
                 $tuan_taskid  =  $_P['tuan_taskid'];
             }
             $tuan_gift_num = $_P['tuan_gift_num'];
             $tuan_gift_id = $_P['tuan_gift_id'];
             $tuan_gift_rule = $_P['tuan_gift_rule'];
             $tuan_gift_remark = $_P['tuan_gift_remark'];
             $tuan_gift_status = $_P['tuan_gift_status'];
             $tuan_check_unique = [];
             $tuan_gift = [];
             if($tuan_gift_num){
                 foreach($tuan_gift_num as $key=>$val){
                     $tuan_gift_field = [];
                     if($tuan_gift_num[$key]&&$tuan_gift_id[$key]&&$tuan_gift_rule[$key]){
                         if(intval($tuan_gift_num[$key])<=0){
                             $msg_array[] = '成团礼:'.$tuan_gift_num[$key].'人成团,人数请大于0';
                         }
                         $check_tuan = $tuan_gift_num[$key].','.$tuan_gift_rule[$key];
                         if(in_array($check_tuan,$tuan_check_unique)){
                             $msg_array[] = '成团礼:'.$tuan_gift_num[$key].'人成团,身份'.$identity[$tuan_gift_rule[$key]].',配置已存在';
                         }
                         $tuan_check_unique[] = $check_tuan;
                         $tuan_gift_field['number'] = $tuan_gift_num[$key];
                         $tuan_gift_field['gift_id'] = $tuan_gift_id[$key];
                         $tuan_gift_field['identity'] = $tuan_gift_rule[$key];
                         if ($tuan_taskid && $tuan_taskid[$key]) {
                             $taskid = intval($tuan_taskid[$key]);
                         } else {
                             $tuan_gift_lastid = $tuan_gift_lastid + 1;
                             $taskid = $tuan_gift_lastid;
                         }
                         $tuan_gift_field['taskid'] = $taskid;
                         $tuan_gift_field['remark'] = $tuan_gift_remark[$key];
                         $tuan_gift_field['status'] = $tuan_gift_status[$key];
                         $tuan_gift[$taskid] = $tuan_gift_field;
                     }
                 }
             }
             $data['tuan_gift'] = json_encode($tuan_gift);

             $open_invite_gift =1;
             $data['open_invite_gift'] = $open_invite_gift;
             $invite_taskid = [];
             if(isset($_P['invite_taskid'])){
                 $invite_taskid  =  $_P['invite_taskid'];
             }
             $invite_gift_num = $_P['invite_gift_num'];
             $invite_gift_id = $_P['invite_gift_id'];
             $invite_gift_rule = $_P['invite_gift_rule'];
             $invite_gift_remark = $_P['invite_gift_remark'];
             $invite_gift_status = $_P['invite_gift_status'];
             $invite_gift = array();
             $invite_check_unique = [];
             if($invite_gift_num){
                 foreach($invite_gift_num as $key=>$val){
                     $invite_gift_field = [];
                     if($invite_gift_num[$key]&&$invite_gift_id[$key]){
                         $invite_gift_field['number']= intval($invite_gift_num[$key]);
                         $invite_gift_field['gift_id']= intval($invite_gift_id[$key]);
                         $invite_gift_field['identity']= intval($invite_gift_rule[$key]);
                         if(intval($invite_gift_num[$key])<=0){
                             $msg_array[] = '邀请下单:'.$invite_gift_num[$key].'人下单,人数请大于0';
                         }
                         $check_invite = $invite_gift_num[$key].','.$invite_gift_rule[$key];
                         if(in_array($check_invite,$invite_check_unique)){
                             $msg_array[] = '邀请下单:'.$invite_gift_num[$key].'人下单,身份'.$identity[$tuan_gift_rule[$key]].',配置已存在';
                         }
                         if($invite_taskid&&$invite_taskid[$key]){
                             $taskid = intval($invite_taskid[$key]);
                         }else{
                             $invite_gift_lastid = $invite_gift_lastid + 1;
                             $taskid = $invite_gift_lastid;
                         }
                         $invite_gift_field['taskid'] = $taskid;
                         $invite_gift_field['remark'] = $invite_gift_remark[$key];
                         $invite_gift_field['status'] = $invite_gift_status[$key];
                         $invite_gift[$taskid] = $invite_gift_field;
                     }
                 }
             }
             $data['invite_gift'] = json_encode($invite_gift);

            //邀请助力、
//             $open_help_gift = $_P['open_help_gift'];
//             $data['open_help_gift'] = $open_help_gift;
//             if($open_help_gift==1){
//                 $help_taskid = [];
//                 if(isset($_P['help_taskid'])){
//                     $help_taskid  =  $_P['help_taskid'];
//                 }
//                 $help_gift_num = $_P['help_gift_num'];
//                 $help_gift_id = $_P['help_gift_id'];
//                 $help_gift_remark = $_P['help_gift_remark'];
//                 $help_gift_status = $_P['help_gift_status'];
//                 $help_invite_identity = $_P['help_invite_identity'];
//                 $help_check_unique = [];
//                 $help_gift = [];
//                 foreach($help_gift_num as $key=>$val){
//                     $help_gift_field = [];
//                     if($help_gift_num[$key]&&$help_gift_id[$key]){
//                         if(intval($help_gift_num[$key])<=0){
//                             $msg_array[] = '邀请助力:'.$help_gift_num[$key].'人助力,人数请大于0';
//                         }
//                         $check_help = $help_gift_num[$key];
//                         if(in_array($check_help,$help_check_unique)){
//                             $msg_array[] = '邀请助力:'.$help_gift_num[$key].'人助力,配置已存在';
//                         }
//                         $help_check_unique[] = $check_help;
//                         $help_gift_field['number'] = $help_gift_num[$key];
//                         $help_gift_field['gift_id'] = $help_gift_id[$key];
//                         $help_gift_field['identity'] = $help_invite_identity;
//                         if ($help_taskid && $help_taskid[$key]) {
//                             $taskid = intval($help_taskid[$key]);
//                         } else {
//                             $help_gift_lastid = $help_gift_lastid + 1;
//                             $taskid = $help_gift_lastid;
//                         }
//                         $help_gift_field['taskid'] = $taskid;
//                         $help_gift_field['remark'] = $help_gift_remark[$key];
//                         $help_gift_field['status'] = $help_gift_status[$key];
//                         $help_gift[$taskid] = $help_gift_field;
//                     }
//                 }
//                 $data['help_gift'] = json_encode($help_gift);
//                 $data['collect_help_user_info'] =  $_P['collect_help_user_info']?intval($_P['collect_help_user_info']):2;
//                 $data['help_user_num'] = $_P['help_user_num']?intval($_P['help_user_num']):0;
//             }else{
//                 $check =  GiftModel::deleteCheckGift($themeid,'help_gift',0);
//                 if(!$check){
//                     show_json(0,['msg'=>'已存在客户邀请记录,不可关闭']);
//                     exit();
//                 }
//                 $data['collect_help_user_info'] = 2;
//                 $data['help_user_num'] = 0;
//             }

             if($msg_array){
                 show_json(0,['msg'=>$msg_array[0]]);
             }

             $data['show_mode'] = $_P['show_mode']?intval($_P['show_mode']):1;
             $data['buoy_img'] = tomedia($_P['buoy_img']);
             $data['exchange_mode'] = $_P['exchange_mode']?intval($_P['exchange_mode']):2;
             $data['is_limit_time'] = $_P['is_limit_time'];
             if($_P['is_limit_time']){
                 $data['limit_start_time']= $_P['gift_limit_time']['start'].':00';
                 $data['limit_end_time'] = $_P['gift_limit_time']['end'].':59';
             }else{
                 $data['limit_start_time'] =date('Y-m-d H:i:s',$theme['begintime']);
                 $data['limit_end_time'] =date('Y-m-d H:i:s',$theme['endtime']);
             }

             $data['tuan_gift_lastid'] =$tuan_gift_lastid;
             $data['invite_gift_lastid'] = $invite_gift_lastid;
             $data['help_gift_lastid'] = $help_gift_lastid;
             if($gift_config){  //编辑
                  $where = ['themeid'=>$themeid,'software'=>MODULE_NAME_TABEL];
                  $data['updated_at'] = date("Y-m-d H:i:s",time());
                  $type = '邀请礼奖品编辑';
                  $old_item = $gift_config;
                  unset($old_item['other_config']);
                  unset($old_item['created_at']);
                  unset($old_item['updated_at']);
                  unset($old_item['tuan_gift_lastid']);
                  unset($old_item['invite_gift_lastid']);
                  unset($old_item['help_gift_lastid']);
                  if(!$_P['open_help_gift']){
                      unset($old_item['help_gift']);
                      unset($old_item['open_help_gift']);
                      unset($old_item['collect_help_user_info']);
                      unset($old_item['help_user_num']);
                  }
                  writelog($themeid,$type,$old_item,$data,$_G['username']);
                  $res = db_update(t_intive_gift_config,$data,$where);
             }else{    //添加
                   $data['software'] = MODULE_NAME_TABEL;
                   $data['themeid'] = $themeid;
                   $data['created_at'] = date('Y-m-d H:i:s',time());
                   yxlog($data,'邀请礼配置添加');
                   $res = $insertid = db_insert(t_intive_gift_config,$data);
             }
             if (!$res){
                 show_json(0,['msg'=>'操作失败']);
             }else{
                 show_json(1,['msg'=>'操作成功']);
             }
             exit();
         }

         if($system['is_multiple_merchants']&& $config['showshop']==0){
            $shangjia_list = GiftModel::getShangjiaList();
         }else{
            $shangjia_list = array();
         }

         $tuan_gift = [];
         $invite_gift = [];
         if($gift_config){
            if($gift_config['tuan_gift']){
                $tuan_gift = json_decode($gift_config['tuan_gift'],true);
            }

            if($gift_config['invite_gift']){
                $invite_gift = json_decode($gift_config['invite_gift'],true);
            }

//            if($gift_config['help_gift']){
//                $help_gift = json_decode($gift_config['help_gift'],true);
//                $first_key = current($help_gift);
//                $gift_config['help_invite_identity'] = $first_key['identity'];
//            }

            if(!$gift_config['limit_start_time']||$gift_config['limit_start_time']=='0000-00-00 00:00:00'){
                $gift_config['limit_start_time'] = date('Y-m-d H:i:s',$theme['begintime']);
            }
            if(!$gift_config['limit_end_time'] ||$gift_config['limit_end_time']=='0000-00-00 00:00:00'){
                $gift_config['limit_end_time'] = date('Y-m-d H:i:s',$theme['endtime']);
            }
         }else{
            $gift_config['limit_start_time'] = date('Y-m-d H:i:s',$theme['begintime']);
            $gift_config['limit_end_time'] = date('Y-m-d H:i:s',$theme['endtime']);
        }

        GiftModel::getUserCanInviteGift($themeid,195);

        $gift_list =  GiftModel::getGiftAllList($themeid);

        if($shangjia_list){
            foreach ($gift_list as $key => $gift){
               if($gift['shangjiaid']){
                   $gift_list[$key]['shangjia_name'] = $shangjia_list[$gift['shangjiaid']]['name'];
               }else{
                   $gift_list[$key]['shangjia_name'] ='';
               }
            }
        }

        $return_clicent = get_defined_vars();
return $this->view('/gift/config', $return_clicent);
    }

    function editGift(){
        global $_G,$_P,$_S,$_C;
        $id = $_P['id'];
        $themeid = intval($_P['themeid']);
        $info = GiftModel::getGiftItemById($themeid,$id);
        show_json(1,['msg'=>'查询成功','data'=>$info]);
    }

    function deleteGift(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        $classify = $_P['classify'];
        $taskid = $_P['taskid'];
        $check =  GiftModel::deleteCheckGift($themeid,$classify,$taskid);
        if(!$check){
            show_json(0,['msg'=>'存在用户获取记录,不可删除,请下架']);
        }else{
            show_json(1,['msg'=>'删除成功,重新提交配置信息生效']);
        }
    }

    function checkConfigGift(){
        global $_G,$_P,$_S,$_C;
        $themeid = intval($_P['themeid']);
        $gift_id = $_P['gift_id'];
        $check =  GiftModel::checkConfigGift($themeid,$gift_id);
        if($check['status']==0){
            show_json(0,$check['msg']);
        }else{
            show_json(1,['msg'=>'无配置']);
        }
    }


    function checkOffGiftConfig(){
        global $_P;
        $themeid = intval($_P['themeid']);
        $classify = $_P['classify'];
        $taskid = 0;
        $check =  GiftModel::deleteCheckGift($themeid,$classify,$taskid);
        if(!$check){
            show_json(0,['msg'=>'存在参与用记录,关闭失败']);
        }else{
            show_json(1,['msg'=>'关闭成功']);
        }
    }


}
